package com.dstz.bpm.engine.listener;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.base.core.util.ThreadMapUtil;
import com.dstz.bpm.act.listener.ActEventListener;
import com.dstz.bpm.api.constant.ActionType;
import com.dstz.bpm.api.constant.NodeType;
import com.dstz.bpm.api.constant.OpinionStatus;
import com.dstz.bpm.api.constant.TaskType;
import com.dstz.bpm.api.engine.context.BpmContext;
import com.dstz.bpm.api.model.task.IBpmTask;
import com.dstz.bpm.core.manager.BpmTaskManager;
import com.dstz.bpm.core.manager.BpmTaskOpinionManager;
import com.dstz.bpm.core.manager.TaskIdentityLinkManager;
import com.dstz.bpm.core.model.BpmTask;
import com.dstz.bpm.core.model.BpmTaskOpinion;
import com.dstz.bpm.engine.action.cmd.DefualtTaskActionCmd;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.impl.ActivitiActivityCancelledEventImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ActivitiCancelledListener implements ActEventListener {
   protected Logger LOG = LoggerFactory.getLogger(this.getClass());
   @Autowired
   BpmTaskManager aJ;
   @Autowired
   TaskCompleteListener ba;
   @Resource
   TaskIdentityLinkManager p;
   @Resource
   BpmTaskOpinionManager j;

   @Override
   public void notify(ActivitiEvent event) {
      ActivitiActivityCancelledEventImpl cancelledEvent = (ActivitiActivityCancelledEventImpl)event;
      DefualtTaskActionCmd taskComplateCmd = (DefualtTaskActionCmd)BpmContext.getActionModel();
      if (taskComplateCmd == null) {
         if (ThreadMapUtil.get("AgileBPMDeleteInstance") != null) {
            return;
         }
      } else if (taskComplateCmd.getActionName().equals(ActionType.MANUALEND.getKey())) {
         return;
      }

      if (NodeType.USERTASK.getKey().equalsIgnoreCase(cancelledEvent.getActivityType())) {
         List<BpmTask> tasks = this.aJ.getByInstIdNodeId(taskComplateCmd.getInstanceId(), cancelledEvent.getActivityId());
         if (tasks.isEmpty() || tasks.size() != 1) {
            throw new BusinessException("流程任务数据异常[" + tasks.size() + " ]：instanceID" + taskComplateCmd.getInstanceId() + "节点：" + cancelledEvent.getActivityId());
         }

         taskComplateCmd.setBpmTask((IBpmTask)tasks.get(0));
         this.ba.b(taskComplateCmd);
         this.a((BpmTask)tasks.get(0));
      }

   }

   private void a(BpmTask task) {
      if (TaskType.SIGN_SOURCE.equalsWithKey(task.getTaskType())) {
         List<BpmTask> taskList = this.aJ.getByParentId(task.getId());
         taskList.forEach((t) -> {
            this.aJ.remove(t.getId());
            BpmTaskOpinion childOpinion = this.j.getByTaskId(t.getId());
            if (childOpinion != null) {
               childOpinion.setStatus(OpinionStatus.CANCELLED.getKey());
               childOpinion.setApproveTime(new Date());
               childOpinion.setDurMs(childOpinion.getApproveTime().getTime() - childOpinion.getCreateTime().getTime());
               childOpinion.setOpinion("任务超时 ，进入边界事件");
               this.j.update(childOpinion);
            }

         });
      }
   }
}
